/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.server;

import cn.ac.iie.di.global.GlobalParas;
import cn.ac.iie.di.httprequest.HttpMessage;
import cn.ac.iie.di.httprequest.MyHttpRequest1;
import cn.ac.iie.di.kill.KillHandler;
import cn.ac.iie.di.msgobject.Message;
import cn.ac.iie.di.rocketmq.RocketMqClient;
import cn.ac.iie.di.rocketmq.RocketMqProducer;
import cn.ac.iie.di.rocketmq.SendToAbandon;
import cn.ac.iie.di.worker.DownPicAction;
import cn.ac.iie.di.worker.DwdPic;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author spf
 */
public class WxGmsgPicDownLoad {

    public static Logger log = Logger.getLogger(WxGmsgPicDownLoad.class.getName());

    public static void main(String[] args) {
        init();
        KillHandler killhandle = new KillHandler();
        killhandle.registerSignal("TERM");
        log.info("init the kill handler done ");

        while (true) {
            try {
                if (!GlobalParas.hasBeenExited) {
                    Thread.sleep(5000);
                } else {
                    log.info("main is exit!!!!");
                    break;
                }
            } catch (Exception ex) {
                log.error(ex, ex);
            }
        }

    }

    public static void init() {
        //init初始化参数,各个配置文件
        GlobalParas.init();
        //回传
        for (int i = 0; i < GlobalParas.httpTheadSize; i++) {
            MyHttpRequest1 myhttprequest1 = new MyHttpRequest1();
            Thread myhttprequest1t = new Thread(myhttprequest1);
            myhttprequest1t.setName("MyHttpRequest1-" + i);
            GlobalParas.httpRequestPool.offer(myhttprequest1);
            myhttprequest1t.start();
            HttpMessage httpmessage = new HttpMessage();
            Thread httpmessaget = new Thread(httpmessage);
            httpmessaget.setName("HttpMessage-" + i);
            GlobalParas.httpMsgQueue.offer(httpmessage);
            httpmessaget.start();
        }

        Monitor monitor = new Monitor();
        Thread tMoni = new Thread(monitor);
        tMoni.setName("Monitor");
        tMoni.start();

        for (int i = 0; i < GlobalParas.parellNumSend; i++) {
            RocketMqProducer rmp = new RocketMqProducer();
            Thread rmpt = new Thread(rmp);
            try {
                GlobalParas.MqProducerQueue.put(rmp);
            } catch (Exception ex) {
                log.error(ex, ex);
            }
            rmpt.setName("RocketMqProducer-" + i);
            rmpt.start();
        }
        for (int i = 0; i < GlobalParas.parellNumSend; i++) {
            SendToAbandon rmp1 = new SendToAbandon();
            Thread ssss = new Thread(rmp1);
            try {
                GlobalParas.MqSendQueue.put(rmp1);
            } catch (Exception ex) {
                log.error(ex, ex);
            }
            ssss.setName("SendToAbandon-" + i);
            ssss.start();
        }
        //下载过程
        for (int i = 0; i < GlobalParas.parellNumDwdA; i++) {
            DownPicAction dpa = new DownPicAction();
            Thread tdpa = new Thread(dpa);
            try {
                GlobalParas.DwdPicAQueue.put(dpa);
            } catch (Exception ex) {
                log.error(ex, ex);
            }
            tdpa.setName("DownPicAction-" + i);
            tdpa.start();
        }

        //获取图片真实url
        for (int i = 0; i < GlobalParas.parellNumDwd; i++) {
            DwdPic dp = new DwdPic();
            Thread dpt = new Thread(dp);
            try {
                GlobalParas.DwdPicQueue.put(dp);
            } catch (InterruptedException ex) {
                log.error(ex, ex);
            }
            dpt.setName("DownLoadPic-" + i);
            dpt.start();
        }

        //将一包里面的消息分解成每个包一个消息
        for (int i = 0; i < GlobalParas.parellNumPae; ++i) {
            Message msg = new Message();
            Thread msgt = new Thread(msg);
            try {
                GlobalParas.ReMsg2MsgQueue.put(msg);
            } catch (Exception ex) {
                log.error(ex, ex);
            }
            msgt.setName("REMsgToMsg-" + i);
            msgt.start();
        }
        //pullmqstart
        for (int i = 0; i < GlobalParas.oriMsgMq.length; i++) {
            RocketMqClient rmc = new RocketMqClient(GlobalParas.oriMsgMq[i]);
            Thread rmct = new Thread(rmc);
            try {
                GlobalParas.pullMsgQueue.put(rmc);
            } catch (Exception ex) {
                log.error(ex, ex);
            }
            rmct.setName("RcoketMqClient-" + GlobalParas.oriMsgMq[i]);
            rmct.start();
        }
    }
}
